/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package knowledgebase.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import knowledgebase.dao.DocumentBlockDAO;
import knowledgebase.dao.DocumentDAO;
import knowledgebase.dao.PermissionDAO;
import knowledgebase.model.AccessType;
import knowledgebase.model.Document;
import knowledgebase.model.DocumentBlock;
import knowledgebase.model.Role;
import knowledgebase.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

/**
 *
 * @author yzaretskyy
 */
@Service("documentBlockService")
public class DocumentBlockServiceImpl implements DocumentBlockService {

    @Autowired
    private DocumentBlockDAO documentBlockDAO;
    @Autowired
    private DocumentDAO documentDAO;
    @Autowired
    private PermissionDAO permissionDAO;
    @Autowired
    private UserService userService;
    
    private ArticleFormatConverter converter = new ArticleFormatConverter();

    @Override
    public DocumentBlock getDocumentBlock(String id) {
        return documentBlockDAO.getDocumentBlock(id);
    }

    @Override
    public void saveDocumentBlock(String docBlockId, String docId, String data, String hierarchy) {
        boolean isNew = (docBlockId == null) ? true : false;
        try {
            docBlockId = (docBlockId == null) ? UUID.randomUUID().toString() : UUID.fromString(docBlockId).toString();
        } catch (Exception ex) {
            docBlockId = UUID.randomUUID().toString();
        }
        String innerDocBlockId = UUID.randomUUID().toString();
        Document document = new Document();
        document.setId(docId);
        DocumentBlock block = new DocumentBlock(docBlockId, converter.convertHTMLToXML(data), document, new Date(), innerDocBlockId, true);
        documentBlockDAO.saveOrUpdateDocumentBlock(block);
        if (isNew && hierarchy != null) {
            //documentDAO.updateDocumentHierarchy(docId, hierarchy);
        }
    }

    @Override
    public void removeDocumentBlockById(String id) {
        documentBlockDAO.removeDocumentBlockById(id);
    }

    @Override
    public List<DocumentBlock> getDocumentBlocksByDocumentId(Document document) {
        List<DocumentBlock> docBlocks = documentBlockDAO.getDocumentBlocksByDocumentId(document);
        for(DocumentBlock block : docBlocks) {
            block.setData(converter.convertXMLtoHTML(block.getData()));
        }
        return converter.sort(docBlocks);
    }
    
    @Override
    public List<DocumentBlock> getDocumentBlocksByDocumentId1(Document document, AccessType accessType) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String ownerName = (principal instanceof Assembler) ? ((Assembler)principal).getUsername() : "";
        User user = userService.findUserByName(ownerName);
        List<DocumentBlock> docBlocks = permissionDAO.getDocumentBlocksForUser(user, document, accessType);
        for(Role role : user.getRoles()) {
            List<DocumentBlock> roleDocBlocks = permissionDAO.getDocumentBlocksForRole(role, document, accessType);
            for(DocumentBlock block : roleDocBlocks) {
                if(!docBlocks.contains(block)) {
                    docBlocks.add(block);
                }
            }
        }
        for(DocumentBlock block : docBlocks) {
            block.setData(converter.convertXMLtoHTML(block.getData()));
        }
        docBlocks = converter.sort(docBlocks);
        return docBlocks;
    }

    @Override
    public void saveAllDocumentBlock(String[] docBlocksData, String[] docBlocksIds, String docId, String hierarchy) {
        for(int i=0; i<docBlocksData.length; i++) {
            saveDocumentBlock(docBlocksIds[i], docId, docBlocksData[i], null);
        }
        documentDAO.updateDocumentHierarchy(docId, hierarchy);
    }
}